package com.stu.serive.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.stu.Dto.StuInformationDto;
import com.stu.mapper.StuInformationMapper;
import com.stu.pojo.*;
import com.stu.serive.StuInformationManageService;
import com.stu.serive.StuInformationService;
import com.stu.vo.StuInformationVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class StuInformationManageServiceImpl implements StuInformationManageService {
    @Autowired
    private StuInformationMapper stuInformationMapper;
    /**
     * 批量删除学生信息
     * @param ids
     */
    /**
     * 菜品批量删除
     * @param ids
     */
    @Transactional
    public void deleteBatch(List<Long> ids) {
        //ids是对应基础表的id，对应family、other表的basic_id
        //根据学生信息id集合，批量删除basic数据
        stuInformationMapper.deleteBasicByIds(ids);
        //根据学生信息id集合，批量删除family数据
        stuInformationMapper.deleteFamilyByBasicIds(ids);
        //根据学生信息id集合，批量删除other数据
        stuInformationMapper.deleteOtherByBasicIds(ids);

    }

    @Override
    public PageBean selectAllStuINF(Integer page, Integer pageSize,
                                    String classInfo, String name, Integer gender, String college, Integer age,
                                    String politicalOutlook, String major) {
        //使用pageheleper
        //1.设置分页参数
        PageHelper.startPage(page, pageSize);
        //2.执行查询
        List<StuInformationVo> stuInformationVoList = stuInformationMapper.selectAllStuINF(
                classInfo, name, gender, college, age,
                politicalOutlook, major);
        for (StuInformationVo stuInformationVo : stuInformationVoList) {
            stuInformationVo.setStuFamilyInformation(
                    stuInformationMapper.selectFamilyById((long) stuInformationVo.getId()));
        }
        Page<StuInformationVo> p = (Page<StuInformationVo>) stuInformationVoList; //将emplist转换类型为page并且封装在p中
        //3.封装PageBean对象
        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }
}
